package com.blacksheep.mtm.controller;

import com.blacksheep.mtm.annotation.SystemControllerLog;
import com.blacksheep.mtm.common.utils.ResultUtil;
import com.blacksheep.mtm.pojo.common.DataTablesResult;
import com.blacksheep.mtm.pojo.common.Result;
import com.blacksheep.mtm.pojo.manager.TbOrder;
import com.blacksheep.mtm.pojo.manager.TbSupplier;
import com.blacksheep.mtm.service.SupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(description= "供应商管理")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    @RequestMapping(value = "/supplier/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取订单列表")
    public DataTablesResult getSupplierList(int draw, int start, int length, String searchKey, int[] state,
                                         String minDate, String maxDate,
                                         @RequestParam("search[value]") String search,
                                         @RequestParam("order[0][column]") int orderCol,
                                         @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"checkbox","supplier_id", "supplier_name","code", "type", "contact", "contact_phone", "create_time","state"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "create_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if (!StringUtils.isBlank(search)){
            searchKey =search;
        }
        DataTablesResult result= supplierService.getSupplierList(draw,start,length,state,searchKey,minDate,maxDate,orderColumn,orderDir);
        return result;
    }
    @RequestMapping(value = "/supplier/del/{ids}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除供应商")
    public Result<Object> deleteBatch(@PathVariable Integer[] ids){
        supplierService.deletBatch(ids);
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/supplier/add",method = RequestMethod.POST)
    @ApiOperation(value = "新增供应商")
    public Result<TbSupplier> insert(TbSupplier supplier){
        TbSupplier result = supplierService.insert(supplier);
        return new ResultUtil<TbSupplier>().setData(result);
    }

    @RequestMapping(value = "/supplier/update",method = RequestMethod.POST)
    @ApiOperation(value = "更新供应商")
    public Result<TbSupplier> update(TbSupplier supplier){
         supplierService.updateSupplier(supplier);
        return new ResultUtil<TbSupplier>().setData(null);
    }


    @RequestMapping(value = "/supplier/stop/{id}",method = RequestMethod.PUT)
    @ApiOperation(value = "下架供应商")
    public Result<TbSupplier> stopMedicine(@PathVariable Integer id){
        TbSupplier medicine = supplierService.alertState(id,-1);
        return new ResultUtil<TbSupplier>().setData(medicine);
    }

    @SystemControllerLog(description = "上架药品")
    @RequestMapping(value = "/supplier/start/{id}",method = RequestMethod.PUT)
    @ApiOperation(value = "上架供应商")
    public Result<TbSupplier> startMedicine(@PathVariable Integer id){
        TbSupplier medicine = supplierService.alertState(id,1);
        return new ResultUtil<TbSupplier>().setData(medicine);
    }
}
